package com.aplink.generic.util;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import com.aplink.generic.controller.GenericApplication;

public class GpsUtils {
    // automatic turn off the gps
    @SuppressWarnings("deprecation")
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static void turnGPSOff() {
        final Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", false);
        GenericApplication.getInstance().getApplicationContext()
                .sendBroadcast(intent);

        String provider = null;
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            provider = Settings.Secure.getString(
                    GenericApplication.getInstance().getApplicationContext()
                            .getContentResolver(),
                    Settings.Secure.LOCATION_MODE);
        } else {
            provider = Settings.Secure.getString(
                    GenericApplication.getInstance().getApplicationContext()
                            .getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        }
        if (provider.contains("gps")) { // if gps is enabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            GenericApplication.getInstance().getApplicationContext()
                    .sendBroadcast(poke);
        }
    }

    @SuppressWarnings("deprecation")
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static void turnGPSOn() {
        final Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", true);
        GenericApplication.getInstance().getApplicationContext()
                .sendBroadcast(intent);

        String provider = null;
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            provider = Settings.Secure.getString(
                    GenericApplication.getInstance().getApplicationContext()
                            .getContentResolver(),
                    Settings.Secure.LOCATION_MODE);
        } else {
            provider = Settings.Secure.getString(
                    GenericApplication.getInstance().getApplicationContext()
                            .getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        }
        if (!provider.contains("gps")) { // if gps is disabled
            final Intent poke = new Intent();
            poke.setClassName("com.android.settings",
                    "com.android.settings.widget.SettingsAppWidgetProvider");
            poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
            poke.setData(Uri.parse("3"));
            GenericApplication.getInstance().getApplicationContext()
                    .sendBroadcast(poke);

        }
    }
}
